package com.vilen.weixin.kit;

import com.vilen.weixin.model.WeixinFinalValue;
import com.vilen.weixin.util.JsonUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * Created by Administrator on 2016/2/10.
 */
public class WeixinKit {
    public static String postReq(String url, String data,String type) {
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        try {
            client = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
            StringEntity entity = new StringEntity(data, ContentType.create(type,"UTF-8"));
            post.addHeader("Content-Type",type);
            post.setEntity(entity);
            response = client.execute(post);
            int status = response.getStatusLine().getStatusCode();
            if(status>=200&&status<300) {
                String str = EntityUtils.toString(response.getEntity());
                //System.out.println(str);
                return str;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(client!=null) {
                    client.close();
                }
                if(response!=null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    public static String getReq(String url,String data,String type) {
        return null;
    }
    public static String getReq(String url) {
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        try {
            client = HttpClients.createDefault();
            HttpGet get = new HttpGet(url);
            response = client.execute(get);
            int status = response.getStatusLine().getStatusCode();
            if(status>=200&&status<300) {
                String str = EntityUtils.toString(response.getEntity(),"UTF-8");
                //System.out.println(str);
                return str;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(client!=null) {
                    client.close();
                }
                if(response!=null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
